﻿using MediatR;
using Microsoft.Extensions.Hosting;
using WMS_Goodsinfo.Api.Read.Application.Command;
using WMS_Goodsinfo.Api.Read.DTOS;
using WMS_Goodsinfo.Domain;
using WMS_Goodsinfo.ErrorCode;
using WMS_Goodsinfo.Infastructure;

namespace WMS_Goodsinfo.Api.Read.Application.Handler
{
    public class SelectWarehousHandler : IRequestHandler<SelectWarehousCommand, ApiPaging<WarehouseDto>>
    {
        private readonly IProductRepository<Product_Table> _productRepo;
        private readonly IProductRepository<Warehouse> _warehouseRepo;

        public SelectWarehousHandler(IProductRepository<Product_Table> productRepo, IProductRepository<Warehouse> warehouseRepo)
        {
            _productRepo = productRepo;
            _warehouseRepo = warehouseRepo;
        }
        /// <summary>
        /// 查询分布库存数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiPaging<WarehouseDto>> Handle(SelectWarehousCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WarehouseDto> result = new ApiPaging<WarehouseDto>();

            var product = _productRepo.GetAll().Where(p => !p.IsDelete);
            var warehouse = _warehouseRepo.GetAll().Where(p => !p.IsDelete);

            // 应用过滤条件
            if (request.Product_Id.HasValue)
            {
                warehouse = warehouse.Where(w => w.Product_Id == request.Product_Id.Value);
            }
          

            if (!string.IsNullOrEmpty(request.Inventory))
            {
                warehouse = warehouse.Where(w => w.Inventory.ToString().Contains(request.Inventory));
            }

            if (!string.IsNullOrEmpty(request.Location))
            {
                warehouse = warehouse.Where(w => w.Location.Contains(request.Location));
            }

            if (!string.IsNullOrEmpty(request.ProductName))
            {
                product = product.Where(p=> p.ProductName.ToString().Contains(request.ProductName));
            }
            if (!string.IsNullOrEmpty(request.ProductTypeId))
            {
                product = product.Where(p => p.ProductTypeId.ToString().Contains(request.ProductTypeId));
            }
            var query = from w in warehouse
                        join p in product on w.Product_Id equals p.Id
                        select new WarehouseDto
                        {
                            Product_Id = w.Product_Id,
                            ProductNo = p.ProductNo,
                            ProductName = p.ProductName,
                            ProductTypeId = p.ProductTypeId,
                            ProductModel = p.ProductModel,
                            UnitName = p.UnitName,
                            Inventory = w.Inventory,
                            Location = w.Location,
                            Current_Inventory =p.Current_Inventory,
                            Available_Inventory = w.Available_Inventory,
                            WaitIn = w.WaitIn,
                            WaitOut = w.WaitOut,
                            Upperlimit = w.Upperlimit,
                            Lowerlimit = w.Lowerlimit
                        };

            // 获取总数
            var totalCount = query.Count();

            // 分页
            var pageData = query
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToList();

            result.Code = StatusEnum.OK;
            result.Message = "查询成功";
            result.TotalCount = totalCount;
            result.PageData = pageData;

            return result;
        }
    }
}
